package com.yonyou.le.unattended.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yonyou.le.unattended.cache.MonitorCacheManager;
import com.yonyou.le.unattended.entity.meta.CarInfoData;
import com.yonyou.le.unattended.service.DeviceDataService;

/**
 * 初始化方法，在服务器启动时执行
 * @author zhangbbd
 */
@Controller
@RequestMapping(value = "/APP")
@Component("BeanDefineConfigue")
public class InitializeControler implements
		ApplicationListener<ContextRefreshedEvent> {// ContextRefreshedEvent为初始化完毕事件，spring还有很多事件可以利用

	@Autowired
	private DeviceDataService deviceService;
	@Autowired
	private MonitorCacheManager mcmCacheManager;
	
	private Logger logger = LoggerFactory.getLogger(InitializeControler.class);
	/**
	 * 当一个ApplicationContext被初始化或刷新触发
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if(!event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")){
			//初始化
			logger.debug("初始化设备缓存....");
			//deviceService.initializeDeviceData();
			testCarInfo();
			logger.debug("初始化设备缓存Finish....");
		}else{
			return;
		}
	}
	@RequestMapping(method=RequestMethod.POST,value="/init")
	public void init(HttpServletRequest request, HttpServletResponse response, Model model){
		logger.debug("初始化设备缓存....");
		deviceService.initializeDeviceData();
		logger.debug("初始化设备缓存Finish....");
	}
	
	/**
	 * 测试
	 */
	private void testCarInfo(){
		CarInfoData data = new CarInfoData();
		data.setId("a1");
		data.setOpcode("ZYD001");
		data.setMaterial("AK47");
		data.setCarno("京A 888888");
		data.setCompany("峨胜集团");
		data.setGrossweight("133");
		data.setTareweight("12.08");
		data.setNetweight("120.92");
		mcmCacheManager.updateCarInfo("ZYD001", data);
		
		data = new CarInfoData();
		data.setId("a1");
		data.setOpcode("ZYD001");
		data.setMaterial("AK47");
		data.setCarno("京A 888888");
		data.setCompany("峨胜集团");
		data.setGrossweight("133");
		data.setTareweight("12.08");
		data.setNetweight("120.92");
		mcmCacheManager.updateCarInfo("ZYD002", data);
		
		data = new CarInfoData();
		data.setId("a1");
		data.setOpcode("ZYD001");
		data.setMaterial("AK47");
		data.setCarno("京A 888888");
		data.setCompany("峨胜集团");
		data.setGrossweight("133");
		data.setTareweight("12.08");
		data.setNetweight("120.92");
		mcmCacheManager.updateCarInfo("ZYD003", data);
		
		data = new CarInfoData();
		data.setId("a1");
		data.setOpcode("ZYD001");
		data.setMaterial("AK47");
		data.setCarno("京A 888888");
		data.setCompany("峨胜集团");
		data.setGrossweight("133");
		data.setTareweight("12.08");
		data.setNetweight("120.92");
		mcmCacheManager.updateCarInfo("ZYD004", data);
		
		data = new CarInfoData();
		data.setId("a1");
		data.setOpcode("ZYD001");
		data.setMaterial("AK47");
		data.setCarno("京A 888888");
		data.setCompany("峨胜集团");
		data.setGrossweight("133");
		data.setTareweight("12.08");
		data.setNetweight("120.92");
		mcmCacheManager.updateCarInfo("ZYD005", data);
		
		data = new CarInfoData();
		data.setId("a1");
		data.setOpcode("ZYD001");
		data.setMaterial("AK47");
		data.setCarno("京A 888888");
		data.setCompany("峨胜集团");
		data.setGrossweight("133");
		data.setTareweight("12.08");
		data.setNetweight("120.92");
		mcmCacheManager.updateCarInfo("ZYD006", data);
		
		data = new CarInfoData();
		data.setId("a1");
		data.setOpcode("ZYD001");
		data.setMaterial("AK47");
		data.setCarno("京A 888888");
		data.setCompany("峨胜集团");
		data.setGrossweight("133");
		data.setTareweight("12.08");
		data.setNetweight("120.92");
		mcmCacheManager.updateCarInfo("ZYD007", data);
		
		data = new CarInfoData();
		data.setId("a1");
		data.setOpcode("ZYD001");
		data.setMaterial("AK47");
		data.setCarno("京A 888888");
		data.setCompany("峨胜集团");
		data.setGrossweight("133");
		data.setTareweight("12.08");
		data.setNetweight("120.92");
		mcmCacheManager.updateCarInfo("ZYD008", data);
	}

}